<?
class TemplateServices{
   var $fieldsort = '';//Sắp xếp theo trường nào
   var $sort		= '';//Sắp xếp tăng hay giảm
   var $page		= 1;//Trang hiện tại
   var $page_size = 4;
   var $page_size_connection = 4;//Kích cỡ 1 trang hiển thị bao nhiêu kết quả
   public function services() {
     $pro = db_array('SELECT *
                     FROM services
                     WHERE ser_active = 1
                     ORDER BY ser_id
                     LIMIT '.($this->page - 1)*$this->page_size.' ,'.$this->page_size);
     return $pro;
   }  
   public function services_total() {
     $arr = db_array('SELECT ser_id
                     FROM services
                     WHERE ser_active = 1
                     ');
     return count($arr);
   }    
   public function ser_detail($ser_id) {
     $arr = db_first('SELECT *
                     FROM services
                     WHERE ser_id ='.$ser_id);
     return $arr;
   }   
   /**
    * ---------------------------------------------------------------------------------------------------------
    * lấy sản phẩm theo danh mục
    * ---------------------------------------------------------------------------------------------------------
   */
   public function ser_cat($ser_cat_id) {
     $arr = db_array('SELECT *
                     FROM services
                     WHERE ser_cat_id IN('.$ser_cat_id.' )
                     ORDER BY ser_id DESC
                     LIMIT '.($this->page - 1)*$this->page_size.' ,'.$this->page_size);
     return $arr;
   }   
   /**
    * ---------------------------------------------------------------------------------------------------------
    * lấy sản phẩm theo danh mục
    * ---------------------------------------------------------------------------------------------------------
   */
   public function ser_cat_total($ser_cat_id) {
     $arr = db_array('SELECT ser_id
                     FROM services
                     WHERE ser_cat_id IN('.$ser_cat_id.' )');
      return count($arr);
   }   
   /**
    * ---------------------------------------------------------------------------------------------------------
    * lấy sản phẩm liên quan (cùng danh mục)
    * ---------------------------------------------------------------------------------------------------------
   */
   public function ser_connection($ser_id,$ser_cat_id) {
     $arr = db_array('SELECT *
                     FROM services
                     WHERE ser_cat_id ='.$ser_cat_id.' AND ser_id != '.$ser_id.' 
                     ORDER BY RAND()
                     LIMIT '.($this->page - 1)*$this->page_size_connection.' ,'.$this->page_size_connection);
     return $arr;
   }   
   /**
    * ---------------------------------------------------------------------------------------------------------
    * lấy id danh mục cha theo danh mục con
    * ---------------------------------------------------------------------------------------------------------
   */
   public function services_cat_parent_id($child_id){
      $cat_parent_id = db_one("SELECT cat_parent_id 
                            FROM categories_multi
                            WHERE cat_type = 'services' AND cat_active = 1 AND cat_id =" . $child_id);
      return $cat_parent_id;
   }
   /**
    * ---------------------------------------------------------------------------------------------------------
    * lấy id danh mục con theo danh mục cha
    * ---------------------------------------------------------------------------------------------------------
   */
   public function services_cat_child_id($parent_id){
      $cat_child_id = db_array("SELECT cat_id 
                            FROM categories_multi
                            WHERE cat_type = 'services' AND cat_active = 1 AND cat_parent_id =" . $parent_id);
      return $cat_child_id;
   }
   /**
    * ---------------------------------------------------------------------------------------------------------
    * lấy danh mục và các danh mục cha tương ứng của sản phẩm
    * ---------------------------------------------------------------------------------------------------------
   */
   public function ser_cat_all($ser_cat_id){
      $str_cat = $ser_cat_id;
      $cat_parent_id = $this->services_cat_parent_id($ser_cat_id);
      $cat_parent_parent_id = $this->services_cat_parent_id($cat_parent_id);
      if($cat_parent_id) $str_cat .= ','.$cat_parent_id;
      if($cat_parent_parent_id) $str_cat .= ','.$cat_parent_parent_id;
      $arr_cat = db_array("SELECT * 
                              FROM categories_multi WHERE cat_id IN(".$str_cat.")");
      return $arr_cat;
   }
   /**
    * ---------------------------------------------------------------------------------------------------------
    * lấy danh mục và tất cả các danh mục con có liên quan tới danh mục được truyền vào
    * ---------------------------------------------------------------------------------------------------------
   */
   public function ser_cat_child_all($ser_cat_id){
      $str_cat = $ser_cat_id;
      $arr_cat_child_id = $this->services_cat_child_id($ser_cat_id);
      if($arr_cat_child_id){
         foreach($arr_cat_child_id as $row){
            $str_cat .= ','.$row['cat_id'];
            $arr_cat_child_child_id = $this->services_cat_child_id($row['cat_id']);
            if($arr_cat_child_child_id){
               foreach($arr_cat_child_child_id as $row1){
                  $str_cat .= ','.$row1['cat_id'];
               }
            }
         }
      }
      return $str_cat;
   }
   public function ser_cat_id_inf($ser_cat_id) {
     $arr = db_first('SELECT *
                     FROM categories_multi
                     WHERE cat_type = "services"
                     AND cat_id = '.$ser_cat_id);
     return $arr;
   }
}
?>